package com.jiayang.hhotel.presenter;


import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.bean.NightAuditModel;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.NightAuditActivity;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.ToastUtil;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 夜审逻辑类
 */

public class NightAuditPresenter extends XPresent<NightAuditActivity> {

    /**
     * 请求夜审列表
     */
    public void asyncListData(int mpLanguage){
        Api.getHomeSer().getNightAuditList(Constant.PHONE,mpLanguage)
                .compose(XApi.<NightAuditModel>getApiTransformer())
                .compose(XApi.<NightAuditModel>getScheduler())
                .compose(getV().<NightAuditModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<NightAuditModel>() {

                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    //正常工作
                    @Override
                    public void onNext(NightAuditModel Results) {
                        LogUtil.showLog("夜审返回="+Results.toString());
                        switch (Results.getState()){
                            case 9://成功
                                getV().setAdapter(Results);
                                break;
                            case -1://失败
                                break;
                            case 1002://没有数据
                                break;
                        }
                    }
                });
    }

    /**
     *
     * 开始夜审
     * @param mpStepsname Filename 存储过程名称
     */
    public void startNightAudit(int mpLanguage, String mpStepsname){
        LogUtil.showLog("stepname="+mpStepsname);
        Api.getHomeSer().startNightAudit(Constant.PHONE, mpLanguage, mpStepsname)//modelList.get(index).getCode()
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel infoModel) {
                        LogUtil.showLog("开始夜审="+infoModel.toString());
                        switch (infoModel.getState()){
                            case 9://请求成功
                                getV().loadSuccess(9);
                                break;
                            case -1://失败
                                getV().loadSuccess(-1);
                                break;
                            case 2007://
                                break;
                        }
                    }
                });
    }
}
